home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Computer Select (Limited Edition)
/
Computer Select.iso
/
pcmag
/
v11n12
/
dlgdemo.exe
/
DLGDEMO3.C
next >
Wrap
Text File
|
1992-04-30
|
30KB
|
784 lines
//
// DLGDEMO3 - Notepad Clone #3 demonstrating use of Win 3.1 Common Dialogs
// Copyright (C) 1992 Ray Duncan
// Ziff Davis Publishing Co. * PC Magazine
//
#define WIN31
#define dim(x) (sizeof(x) / sizeof(x[0])) // returns no. of elements
#define EXENAMESIZE 256 // max length of path+filename
#define BUFSIZE 65520 // max length of file data
#include "string.h"
#include "windows.h"
#include "commdlg.h"
#include "dlgdemo3.h"
HANDLE hInst; // module instance handle
HWND hFrame; // handle for frame window
HWND hEdit; // handle for edit window
char szFileName[EXENAMESIZE+1]; // name of current file
char szTemp[EXENAMESIZE+1]; // filename scratch buffer
int hFile; // handle for current file
HANDLE hBuff; // handle for file I/O buffer
LPSTR lpBuff; // far pointer to file buffer
DWORD dwColor = RGB(0, 0, 0); // user-selected colorref
// for FindText & ReplaceText
FINDREPLACE fr; // common dialog data structure
HWND hFindDlg = (HWND) 0; // nomodal dialog handle
char szFind[256]; // text to find
char szReplace[256]; // text to replace
char szShortAppName[] = "DlgDemo"; // short application name
char szAppName[] = "Common Dialog Demo #3"; // long application name
char szMenuName[] = "DlgDemoMenu"; // name of menu resource
char szDefName[] = "UNTITLED"; // default filename
char szDefExt[] = "TXT"; // default extension
char *szFilter[] = { // filters for Open and
"ASCII Text (*.TXT)", "*.TXT", // SaveAs common dialogs
"All Files (*.*)", "*.*",
"" };
struct decodeWord { // structure associates
WORD Code; // messages or menu IDs
LONG (*Fxn)(HWND, WORD, WORD, LONG); }; // with a function
//
// Table of window messages supported by FrameWndProc()
// and the functions which correspond to each message.
//
struct decodeWord messages[] = {
0, DoFindReplace,
WM_CREATE, DoCreate,
WM_INITMENU, DoInitMenu,
WM_SETFOCUS, DoSetFocus,
WM_SIZE, DoSize,
WM_COMMAND, DoCommand,
WM_DESTROY, DoDestroy,
WM_CTLCOLOR, DoSetColor, } ;
//
// Table of menubar item IDs and their corresponding functions.
//
struct decodeWord menuitems[] = {
IDM_NEW, DoMenuNew,
IDM_OPEN, DoMenuOpen,
IDM_SAVE, DoMenuSave,
IDM_SAVEAS, DoMenuSaveAs,
IDM_EXIT, DoMenuExit,
IDM_UNDO, DoMenuUndo,
IDM_CUT, DoMenuCut,
IDM_COPY, DoMenuCopy,
IDM_PASTE, DoMenuPaste,
IDM_DELETE, DoMenuDelete,
IDM_FIND, DoMenuFind,
IDM_REPLACE, DoMenuReplace,
IDM_FONT, DoMenuFont,
IDM_COLOR, DoMenuColor, } ;
//
// WinMain -- entry point for this application from Windows.
//
int PASCAL WinMain(HANDLE hInstance,
HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
hInst = hInstance; // save this instance handle
if(!hPrevInstance) // if first instance,
if(!InitApplication(hInstance)) // register window class
{
MessageBox(hFrame, "Can't initialize application!", szAppName,
MB_ICONSTOP|MB_OK);
return(FALSE);
}
if(!InitInstance(hInstance, nCmdShow)) // create this instance's window
{
MessageBox(hFrame, "Can't initialize instance!", szAppName,
MB_ICONSTOP|MB_OK);
return(FALSE);
}
while(GetMessage(&msg, NULL, 0, 0)) // while message != WM_QUIT
{
if((hFindDlg == 0) || !IsDialogMessage(hFindDlg, &msg))
{
TranslateMessage(&msg); // translate virtual key codes
DispatchMessage(&msg); // dispatch message to window
}
}
TermInstance(hInstance); // clean up for this instance
return(msg.wParam); // return code = WM_QUIT value
}
//
// InitApplication --- global initialization code for this application.
//
BOOL InitApplication(HANDLE hInstance)
{
WNDCLASS wc;
// set parameters for frame window class
wc.style = CS_HREDRAW|CS_VREDRAW; // class style
wc.lpfnWndProc = FrameWndProc; // class callback function
wc.cbClsExtra = 0; // extra per-class data
wc.cbWndExtra = 0; // extra per-window data
wc.hInstance = hInstance; // handle of class owner
wc.hIcon = LoadIcon(hInst, "DlgDemoIcon"); // application icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // default cursor
wc.hbrBackground = GetStockObject(WHITE_BRUSH); // background color
wc.lpszMenuName = szMenuName; // name of menu resource
wc.lpszClassName = szShortAppName; // name of window class
return(RegisterClass(&wc)); // register class, return status
}
//
// InitInstance --- instance initialization code for this application.
//
BOOL InitInstance(HANDLE hInstance, WORD nCmdShow)
{
hFrame = CreateWindow( // create frame window
szShortAppName, // window class name
szAppName, // text for title bar
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, CW_USEDEFAULT, // default position
CW_USEDEFAULT, CW_USEDEFAULT, // default size
NULL, // no parent window
NULL, // use class default menu
hInstance, // window owner
NULL); // unused pointer
if(!hFrame) return(FALSE); // error, can't create window
hBuff = GlobalAlloc(GMEM_MOVEABLE, BUFSIZE); // allocate memory
if(!hBuff) // abort if no memory
{
MessageBox(hFrame, "Can't allocate memory!", szAppName,
MB_ICONSTOP|MB_OK);
return(0);
}
lpBuff = GlobalLock(hBuff); // get far pointer to memory
// register message for FindText() and ReplaceText() common dialogs
messages[0].Code = RegisterWindowMessage((LPSTR) FINDMSGSTRING);
ShowWindow(hFrame, nCmdShow); // make frame window visible
UpdateWindow(hFrame); // force WM_PAINT message
SendMessage(hFrame, WM_COMMAND, // force new (empty) file by
IDM_NEW, 0L); // simulating File-New command
return(TRUE); // return success flag
}
//
// TermInstance -- instance termination code for this application.
//
BOOL TermInstance(HANDLE hinstance)
{
GlobalUnlock(hBuff); // unlock the memory buffer
GlobalFree(hBuff); // release the buffer
return(TRUE); // return success flag
}
//
// FrameWndProc --- callback function for application frame window.
//
LONG FAR PASCAL FrameWndProc(HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
{
int i; // scratch variable
for(i = 0; i < dim(messages); i++) // decode window message and
{ // run corresponding function
if(wMsg == messages[i].Code)
return((*messages[i].Fxn)(hWnd, wMsg, wParam, lParam));
}
return(DefWindowProc(hWnd, wMsg, wParam, lParam));
}
//
// DoCreate -- process WM_CREATE message for frame window by
// creating a multiline edit control that will fill the frame window.
//
LONG DoCreate(HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
{
hEd